import torch
import torch.nn as nn

# 定义模型输出和目标标签
log_probs = torch.tensor([[ -0.5, -0.2, -1.0],   # 样本1的对数概率
                          [ -0.3, -0.7, -0.1],
                          [ -0.3, -0.5, -0.1]],  # 样本2的对数概率
                        dtype=torch.float32)
targets = torch.tensor([1, 2, 1], dtype=torch.int64) # 目标标签

# 创建 NLLLoss 对象
criterion = nn.NLLLoss()

# 计算损失
loss = criterion(log_probs, targets)

print(loss.item())  # 输出损失值
